using System;
using System.Collections.Generic;
using System.Text;
using GetSharePointToValidate;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace GetSharePointToValidate.FeatureCollection
{
    public class FeatureReceiverModifierCompatBrower : SPFeatureReceiver
    {
        private const string jobName = "ModifierCompatBrowerJobs";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
            AddJob(webApplication, true);
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication; 
            AddJob(webApplication, false);
        }

        public static void AddJob(SPWebApplication WebApplication, bool AddOrRemove)
        {
            RemoveJob(WebApplication);
            DeploymentJobs.ModifierCompatBrowerJobs mcbj = new DeploymentJobs.ModifierCompatBrowerJobs(jobName, WebApplication);
            mcbj.AddModifierOrRemoveModifier = AddOrRemove;
            SPOneTimeSchedule oneTimeSchedule = new SPOneTimeSchedule();
            oneTimeSchedule.Time = DateTime.Now.AddSeconds(10);
            mcbj.Schedule = oneTimeSchedule;
            mcbj.Update();
        }
        public static void RemoveJob(SPWebApplication WebApplication)
        {
            // clearing up the job
            foreach (SPJobDefinition job in WebApplication.JobDefinitions)
            {
                if (job.Name == jobName)
                    job.Delete();
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }
    }
}
